package com.basiclib.base

//import org.greenrobot.eventbus.EventBus
import android.content.Context
import android.os.Bundle
import android.os.Looper
import androidx.appcompat.app.AppCompatActivity
import android.util.Log
import android.view.inputmethod.InputMethodManager
import com.basiclib.inter.IActivity
import com.basiclib.inter.IMvpView
import com.basiclib.inter.IPresenter
import com.basiclib.lifecycle.ActivityLifecycleable
import com.basiclib.utils.DialogManager
import com.basiclib.utils.STATUS_ERROR
import com.basiclib.utils.STATUS_SUCESS
import com.basiclib.utils.eventbus.EventBusMessage
import com.basiclib.utils.isNetworkAvaliable
import com.basiclib.view.ProgressDialog
import com.basiclib.view.SnackbarUtil
import com.basiclib.widget.OnDialogDismissListener
import com.chad.library.adapter.base.loadmore.LoadMoreView.STATUS_LOADING
import com.trello.rxlifecycle2.android.ActivityEvent
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.toast

abstract class BaseActivity : AppCompatActivity(), IActivity, IMvpView,
    ActivityLifecycleable {
    val TAG = this::class.java.simpleName
    val behaviorSubject = BehaviorSubject.create<ActivityEvent>()
    var progressDialog: ProgressDialog = ProgressDialog()


    override fun provideLifecycleSubject(): Subject<ActivityEvent> = behaviorSubject

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getContentViewResId())
        onActivityInject()
        attachView(savedInstanceState)
        initView()
        setListener()
        initData()
    }

    override fun onResume() {
        super.onResume()
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
    }

    abstract fun getContentViewResId(): Int

    protected abstract fun onActivityInject()

    protected abstract fun attachView(savedInstanceState: Bundle?)

    abstract fun getPresenter(): IPresenter<*>?

    abstract fun initView()

    abstract fun initData()

    override fun setListener() {}


    override fun isNetWorkAvailable(): Boolean = isNetworkAvaliable(this)

    override fun useFragment() = false

    override fun showLoading() {
    }

    override fun hideLoading() {
    }

    override fun showErrorView() {
    }

    override fun showLoading(msg: String, error: String, listener: OnDialogDismissListener?) {
        showLoading(this, msg, errorMsg = error, listener = listener)
    }

    override fun showErrorLoading(error: String, listener: OnDialogDismissListener?) {
        showLoading(this, status = STATUS_ERROR, errorMsg = error, listener = listener)
    }

    override fun showSucessLoading(msg: String, listener: OnDialogDismissListener?) {
        showLoading(this, msg = msg, status = STATUS_SUCESS, listener = listener)
    }

    fun showLoading(
        ctx: Context,
        msg: String = "",
        status: Int = STATUS_LOADING,
        errorMsg: String = "",
        tag: Int = -1,
        listener: OnDialogDismissListener? = null
    ) {
        DialogManager.instance().showProgressBarDialog(ctx, msg, status, errorMsg, tag, listener)
    }


    fun showLoadingDialog() {
        if (progressDialog.getTag() == null) {
            progressDialog.show(supportFragmentManager, "progress")
        }
    }

    fun dismissLoadingDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss()
        }
    }

    override fun showShortToast(msg: String) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            runOnUiThread { toast(msg) }
        } else {
            toast(msg)
        }
    }

    fun showShortToast(message: String, type: Int) {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm?.hideSoftInputFromWindow(window.decorView.windowToken, 0)
        SnackbarUtil.showBarShortTime(findViewById(android.R.id.content), message, type)
    }


    override fun onPause() {
        super.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        getPresenter()?.onDestroy()
    }

    @Subscribe
    fun onEventMainThread(eventBusMessage: EventBusMessage) {
        Log.i(TAG, "onEventMainThread: " + eventBusMessage.toString())
    }
}